﻿#include "scoreboard.h"

Scoreboard::Scoreboard(QWidget *parent) : QWidget(parent)
{
    // 创建标签
    scoreLabel = new QLabel(u8"分数: 0", this);
    levelLabel = new QLabel(u8"等级: 1", this);
    linesLabel = new QLabel(u8"已消行: 0", this);
    nextPieceLabel = new QLabel(u8"下一个方块:", this);

    // 设置字体和颜色
    QFont labelFont("Arial", 14, QFont::Bold);
    QPalette palette;
    palette.setColor(QPalette::WindowText, Qt::white);

    scoreLabel->setFont(labelFont);
    scoreLabel->setPalette(palette);
    levelLabel->setFont(labelFont);
    levelLabel->setPalette(palette);
    linesLabel->setFont(labelFont);
    linesLabel->setPalette(palette);
    nextPieceLabel->setFont(labelFont);
    nextPieceLabel->setPalette(palette);

    // 创建按钮
    startButton = new QPushButton(u8"开始游戏", this);
    pauseButton = new QPushButton(u8"暂停", this);

    // 设置按钮样式
    QString buttonStyle = "QPushButton {"
                          "  background-color: #27ae60;"
                          "  color: white;"
                          "  border: none;"
                          "  border-radius: 5px;"
                          "  padding: 10px;"
                          "  font-weight: bold;"
                          "}"
                          "QPushButton:hover { background-color: #2ecc71; }"
                          "QPushButton:pressed { background-color: #219653; }";

    startButton->setStyleSheet(buttonStyle);
    pauseButton->setStyleSheet(buttonStyle.replace("#27ae60", "#f39c12")
                               .replace("#2ecc71", "#f1c40f")
                               .replace("#219653", "#e67e22"));

    // 创建用于显示下一个方块的QLabel
    nextPieceDisplay = new QLabel(this);
    nextPieceDisplay->setFixedSize(100, 100);
    nextPieceDisplay->setStyleSheet("background-color: #2c3e50; border: 1px solid #34495e;");
    nextPieceDisplay->setAlignment(Qt::AlignCenter);  // 内容居中


    // 布局
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(scoreLabel);
    layout->addWidget(levelLabel);
    layout->addWidget(linesLabel);
    layout->addSpacing(20);
    layout->addWidget(nextPieceLabel);
     layout->addWidget(nextPieceDisplay);
    layout->addSpacing(20);
    layout->addWidget(startButton);
    layout->addWidget(pauseButton);
    layout->addStretch(1);

    setLayout(layout);

    // 连接信号槽
    connect(startButton, &QPushButton::clicked, this, &Scoreboard::startGame);
    connect(pauseButton, &QPushButton::clicked, this, &Scoreboard::pauseGame);

    // 设置背景
    setStyleSheet("background-color: #34495e;");
}

void Scoreboard::setNextPiece(const TetrisPiece &piece)
{
    nextPiece = piece;
    drawNextPiece();
}

void Scoreboard::setScore(int score)
{
    scoreLabel->setText(QString(u8"分数: %1").arg(score));
}

void Scoreboard::setLevel(int level)
{
    levelLabel->setText(QString(u8"等级: %1").arg(level));
}

void Scoreboard::setLinesRemoved(int count)
{
    linesLabel->setText(QString(u8"已消行: %1").arg(count));
}

void Scoreboard::drawNextPiece()
{
    QPixmap pixmap(nextPieceDisplay->size());
    pixmap.fill(Qt::transparent);

    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);

    // 计算绘制位置（居中显示）
    int blockSize = 20;
    int centerX = pixmap.width() / 2;
    int centerY = pixmap.height() / 2;

    // 绘制下一个方块
    for (int i = 0; i < 4; ++i) {
        int x = centerX + nextPiece.x(i) * blockSize;
        int y = centerY + nextPiece.y(i) * blockSize;
        painter.fillRect(x, y, blockSize, blockSize, nextPiece.color());
    }

    nextPieceDisplay->setPixmap(pixmap);
}
